<?php

namespace app\common;

abstract class BaseTableController extends BaseAdminController {
	public function index() {
		$this->assign ( 'page', $this->request->param ( 'page', 1 ) );
		return $this->fetch ();
	}
	
	/**
	 * jqgrid 加载数据
	 */
	public function getData() {
		$page = $this->_getPage ( $this->request->param ( 'rows', 20 ) );
		// 返回数据
		$result = [ ];
		$result ['page'] = $page->currentPage ();
		$result ['total'] = $page->total ();
		$result ['rows'] = $page->all ();
		return $this->json ( $result );
	}
	
	/**
	 * jqgrid 数据操作
	 */
	public function save() {
		$result = new RenderJson (); // 返回数据
		$oper = $this->request->param ( 'oper', false );
		if ($oper == 'add') {
			if ($this->_addHandler ( $this->request->param () )) {
				$result->success ( true, '新增成功' );
			} else {
				$result->msg ( '新增失败' );
			}
		} else if ($oper == 'edit') {
			if ($this->_editHandler ( $this->request->param () )) {
				$result->success ( true, '更新成功' );
			} else {
				$result->msg ( '更新失败' );
			}
		} else if ($oper == 'del') {
			if ($this->_delHandler ( $this->request->param ( 'id' ) )) {
				$result->success ( true, '删除成功' );
			} else {
				$result->msg ( '删除失败' );
			}
		} else {
			$result->msg ( '未知请求类型' );
		}
		return $this->json ( $result );
	}
	/**
	 * 获取分页数据,请使用 paginate 方法
	 *
	 * @param int $pageSize
	 */
	abstract function _getPage($pageSize);
	/**
	 * 新增操作
	 *
	 * @param unknown $param
	 */
	abstract function _addHandler($param);
	/**
	 * 编辑操作
	 *
	 * @param unknown $param
	 */
	abstract function _editHandler($param);
	/**
	 * 删除操作
	 *
	 * @param unknown $id
	 */
	abstract function _delHandler($id);
}